<?php

require_once("report.php");

class Specific_daily extends Report {

    function __construct() {
        parent::__construct();
    }

    public function getDataColumns() {
        return array('summary' => array(
                array('data' => lang('reports_date'), 'align' => 'left'),
                array('data' => lang('reports_invoice_num'), 'align' => 'left'),
                array('data' => lang('reports_name'), 'align' => 'left'),
                array('data' => lang('reports_description'), 'align' => 'left'),
                array('data' => lang('reports_serial_number'), 'align' => 'left'),
                array('data' => lang('reports_quantity_purchased'), 'align' => 'left'),
                array('data' => lang('reports_price'), 'align' => 'left'),
                array('data' => lang('reports_subtotal'), 'align' => 'right'),
                array('data' => 'Cash / Credit', 'align' => 'left'),
                array('data' => 'staff', 'align' => 'left'),
            )
        );
    }

    public function getData() {
        $this->db->select('sale_id, sale_date,items.name as item_name,items.unit_price as price,serialnumber,payment_type,sales_items_temp.deleted as deleted, sales_items_temp.description, quantity_purchased, subtotal,total, tax,CONCAT(first_name," ",last_name) as staff', false);
        $this->db->from('sales_items_temp');
        $this->db->join('items', 'sales_items_temp.item_id = items.item_id', 'left');
        $this->db->join('item_kits', 'sales_items_temp.item_kit_id = item_kits.item_kit_id', 'left');
        $this->db->join('employees', 'sales_items_temp.employee_id = employees.person_id', 'left');
        $this->db->join('people', 'employees.person_id = people.person_id', 'left');
        if (isset($this->params['employee_id']) && $this->params['employee_id']) {
            $this->db->where('employees.person_id = ' . $this->params['employee_id']);
        }
        if ($this->params['sale_type'] == 'sales') {
            $this->db->where('quantity_purchased > 0');
        } elseif ($this->params['sale_type'] == 'returns') {
            $this->db->where('quantity_purchased < 0');
        }
        $this->db->where('sales_items_temp.deleted', 0);
        $this->db->order_by('staff ASC');
        $this->db->order_by('sale_date DESC');
        $this->db->order_by('sale_id DESC');
        $data = array();
        $data['summary'] = $this->db->get()->result_array();
        return $data;


        $this->db->select('sale_id, sale_date', false);
        $this->db->from('sales_items_temp');
        $this->db->join('people', 'sales_items_temp.customer_id = people.person_id', 'left');
        $this->db->where('employee_id = ' . $this->params['employee_id']);

        if ($this->params['sale_type'] == 'sales') {
            $this->db->where('quantity_purchased > 0');
        } elseif ($this->params['sale_type'] == 'returns') {
            $this->db->where('quantity_purchased < 0');
        }
        $this->db->where('deleted', 0);
        $this->db->group_by('sale_id');
        $this->db->order_by('sale_date');

        $data = array();
        $data['sales'] = $this->db->get()->result_array();
        $data['summary'] = array();
        foreach ($data['sales'] as $key => $value) {
            $this->db->select('CONCAT(first_name," ",last_name) as staff,sale_id, sale_date,items.name as item_name,items.serial_number as seri,items.unit_price as price, serialnumber,payment_type, sales_items_temp.description, quantity_purchased, subtotal,total, tax');
            $this->db->from('sales_items_temp', 'people');
            $this->db->join('items', 'sales_items_temp.item_id = items.item_id', 'left');
            $this->db->join('item_kits', 'sales_items_temp.item_kit_id = item_kits.item_kit_id', 'left');
            $this->db->join('employees', 'sales_items_temp.employee_id = employees.person_id', 'left');
            $this->db->join('people', 'employees.person_id = people.person_id', 'left');
            $this->db->where('sale_id = ' . $value['sale_id']);
            $items = $this->db->get()->result_array();
            $data['summary'] = array_merge_recursive($items, $data['summary']);
        }
        return $data;
    }

    public function getSummaryData() {
        $this->db->select('sum(quantity_purchased) as quality,sum(total) as total, sum(subtotal) as subtotal');
        $this->db->from('sales_items_temp');
        if (isset($this->params['employee_id']) && $this->params['employee_id']) {
            $this->db->where('employee_id = ' . $this->params['employee_id']);
        }
        if ($this->params['sale_type'] == 'sales') {
            $this->db->where('quantity_purchased > 0');
        } elseif ($this->params['sale_type'] == 'returns') {
            $this->db->where('quantity_purchased < 0');
        }
        $this->db->where('deleted', 0);
        return $this->db->get()->row_array();
    }

}

?>